EEL_MODS	+= optee_examples

# optee examples
DEPS_TEE += optee_examples
$(eval $(call deftargets_git,optee_examples,OTEX))
$(eval $(call deftargets_download,optee_examples,OTEX,$$(OPTEE_VERSION),$$(OPTEE_VERSION)))

# original path basename, such as 'hello_world'
_OTEX_SUBDIRS := $(sort $(patsubst $(S_OTEX_DIR)/%/Makefile,%,\
				$(wildcard $(S_OTEX_DIR)/*/Makefile)))
# targets
OTEX_SUBDIRS := $(addprefix otex-,$(_OTEX_SUBDIRS))

OTEX_CAS	:= $(addsuffix -host,$(OTEX_SUBDIRS))
OTEX_TAS	:= $(addsuffix -ta,$(OTEX_SUBDIRS))

PHONY-optee_examples += otex-% otex-%-host otex-%-ta
PHONY += $(OTEX_SUBDIRS) $(OTEX_CAS) $(OTEX_TAS)

OTEX_DEPS := $(O_OTOS_DIR)/.build_done.optee_os
OTEX_DEPS += $(O_OTCL_DIR)/.build_done.optee_client

# build ca and ta
$(OTEX_SUBDIRS): $(OTEX_DEPS) optee_examples-prepare
	$(MAKE) $(JOB_OPT) \
		-C $(S_OTEX_DIR)/$(patsubst otex-%,%,$@) \
		CROSS_COMPILE=$(TC_PREFIX) \
		TA_DEV_KIT_DIR=$(O_OTOS_DIR)/export-ta_$(OPTEE_ARCH2) \
		TEEC_EXPORT=$(O_OTCL_DIR)/export/usr \
		PLATFORM=$(OPTEE_PLAT)

# build ca
$(OTEX_CAS): $(OTEX_DEPS) optee_examples-prepare
	$(MAKE) $(JOB_OPT) \
		--no-builtin-variables \
		-C $(S_OTEX_DIR)/$(patsubst otex-%-host,%,$@)/host \
		CROSS_COMPILE=$(TC_PREFIX) \
		TEEC_EXPORT=$(O_OTCL_DIR)/export/usr

# build ta
$(OTEX_TAS): $(OTEX_DEPS) optee_examples-prepare
	$(MAKE) $(JOB_OPT) \
		-C $(S_OTEX_DIR)/$(patsubst otex-%-ta,%,$@)/ta \
		CROSS_COMPILE=$(TC_PREFIX) \
		TA_DEV_KIT_DIR=$(O_OTOS_DIR)/export-ta_$(OPTEE_ARCH2) \
		TEEC_EXPORT=$(O_OTCL_DIR)/export/usr \
		PLATFORM=$(OPTEE_PLAT)

PHONY-optee_examples += optee_examples optee_examples-clean

# FIXME: can not use '$(OTEX_CAS) $(OTEX_TAS)' take place '$(OTEX_SUBDIRS)'
# FIXME: can not use '$(MAKE) $(JOB_OPT) prepare-for-rootfs -C ...'
# HOST_CROSS_COMPILE used by $(S_OTEX_DIR)/Makefile
# see 'tee-supplicant -h' for install dirs
optee_examples: $(OTEX_DEPS) optee_examples-prepare $(OTEX_SUBDIRS)
	$(EELQ)$(MAKE) $(JOB_OPT) prepare-for-rootfs \
		--no-print-directory \
		-C $(S_OTEX_DIR) \
		HOST_CROSS_COMPILE=$(TC_PREFIX) \
		TA_DEV_KIT_DIR=$(O_OTOS_DIR)/export-ta_$(OPTEE_ARCH2)

	$(EELQ4)\rm -fr $(O_OTEX_DIR)
	$(EELQ4)mkdir -p $(O_OTEX_DIR)

	$(EELQ3)mkdir -p $(O_OTEX_DIR)/optee_example_ca
	$(EELQ3)mkdir -p $(O_OTEX_DIR)/rfs_overlay/lib/optee_armtz
	$(EELQ3)mkdir -p $(O_OTEX_DIR)/rfs_overlay/usr/lib/tee-supplicant/plugins

	$(EELQ3)cp -afr $(S_OTEX_DIR)/out/ca/optee_example_* $(O_OTEX_DIR)/optee_example_ca
	$(EELQ3)cp -afr $(S_OTEX_DIR)/out/ta/*.ta            $(O_OTEX_DIR)/rfs_overlay/lib/optee_armtz
	$(EELQ3)cp -afr $(S_OTEX_DIR)/out/plugins/*          $(O_OTEX_DIR)/rfs_overlay/usr/lib/tee-supplicant/plugins

	$(EELQ4)touch $(O_OTEX_DIR)/.build_done.optee_examples

# examples-clean left something
optee_examples-clean: $(OTEX_DEPS)
	$(EELQ4)\rm -f $(O_OTEX_DIR)/.build_done.optee_examples
	$(EELQ4)\rm -fr $(O_OTEX_DIR)
	$(EELQ)$(MAKE) $(JOB_OPT) examples-clean prepare-for-rootfs-clean \
		--no-print-directory \
		-C $(S_OTEX_DIR) \
		CROSS_COMPILE=$(TC_PREFIX) \
		TA_DEV_KIT_DIR=$(O_OTOS_DIR)/export-ta_$(OPTEE_ARCH2) \
		TEEC_EXPORT=$(O_OTCL_DIR)/export/usr \
		PLATFORM=$(OPTEE_PLAT)

$(O_OTEX_DIR)/.build_done.optee_examples:
	@$(MAKE_THIS) optee_examples

